<!DOCTYPE html>
<html lang="en">

<head>
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Document</title>
</head>

<body>
    <script>

        function getP(n) {
            const res = []
            const path = []
            const map = {
                "(": 1,
                ")": -1
            }
            const mapKeys = Object.keys(map)
            let count = 0
            const total = 2 * n
            const getK = (start) => {
                if (count < 0 || start > total) {
                    return
                }
                if (start === total && count === 0) {
                    const str = [...path].join('')
                    res.push(str)
                    return
                }

                for (let i = 0; i < mapKeys.length; i++) {
                    path.push(mapKeys[i])
                    count += map[mapKeys[i]]
                    getK(start + 1)
                    path.pop()
                    count -= map[mapKeys[i]]
                }
            }
            getK(0)
            return res

        }
        console.log(
            getP(3)
        )
    </script>

</body>

</html>